package com.mac.intellect_flow.admin.controller.common;

import cn.hutool.core.lang.UUID;
import com.mac.intellect_flow.constant.CommonConstant;
import com.mac.intellect_flow.framework.util.JwtUtil;
import com.mac.intellect_flow.redis.util.RedisUtil;
import com.mac.intellect_flow.reply.R;
import com.mac.intellect_flow.sys.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 验证码操作处理
 * 
 * @author mac
 */
@RestController
public class CaptchaController {
    @Autowired
    private RedisUtil redisCache;
    
    @Autowired
    private SysConfigService configService;
    /**
     * 生成验证码
     */
    @GetMapping("/captcha")
    public R getCode(HttpServletResponse response) {
        String uuid = UUID.fastUUID().toString();
        String verifyKey = CommonConstant.CAPTCHA_CODE_KEY + uuid;
        Random random = new Random();
        String formattedNumber = String.format("%06d", random.nextInt(1000000));
        JwtUtil.redisUtil.setCacheObject(verifyKey, formattedNumber, CommonConstant.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        Map<String,Object> data = new HashMap<>();
        data.put("uuid", uuid);
        data.put("code", formattedNumber);
        return R.success(data);
    }
}
